package com.yaojiaxun.logisticsPlatformSystem.controller;

import com.yaojiaxun.logisticsPlatformSystem.service.AreaService;
import com.yaojiaxun.logisticsPlatformSystem.util.ResultVOUtil;
import com.yaojiaxun.logisticsPlatformSystem.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @Auther: yjx_j
 * @Date: 2019/1/13 15:12
 * @Description:
 */
@Controller
public class AreaController {

    @Autowired
    private AreaService areaService;

    //地址基本信息管理首页
    @RequestMapping("/area_manage")
    public String AreaManageIndex(@RequestParam(defaultValue = "1") Integer page, Map<String, Object> map){
//        map.put("provinces", areaService.findAllProvinces());
        map.put("pageDTO", areaService.findPage(page));
        return "system/area/area_manage";
    }

    @ResponseBody
    @RequestMapping("/area_manage_json")
    public ResultVO AreaManageIndexJson(@RequestParam(defaultValue = "1") Integer page){
        return ResultVOUtil.success(areaService.findPage(page));
    }

    @RequestMapping("/area_manage/cityLevel")
    public String getCityLevel(@RequestParam("parentId") Integer parentId, Map<String, Object> map){
        map.put("parentId",parentId);
        map.put("name", areaService.findCityNameById(parentId));
        map.put("cityLevels", areaService.findCityLevelByParentId(parentId));
        return "system/area/area_city_level";
    }

    @RequestMapping("/area_manage/countryLevel")
    public String getCountryLevel(@RequestParam("parentId") Integer parentId, Map<String, Object> map){
        map.put("parentId", parentId);
        map.put("name", areaService.findCityNameById(parentId));
        map.put("CityLevelId", areaService.findParentIdById(parentId));
        map.put("parentName", areaService.findCityNameById(areaService.findParentIdById(parentId)));
        map.put("countryLevels", areaService.findCountryLevelByParentId(parentId));
        return "system/area/area_country_level";
    }

    @ResponseBody
    @RequestMapping("/getProvince")
    public ResultVO getProvince(){
        return ResultVOUtil.success(areaService.findAllProvinces());
    }

    @ResponseBody
    @RequestMapping("/getCities")
    public ResultVO getCities(@RequestParam("parentCity") String parentCity){
        Integer parentId = areaService.findAreaIdByName(parentCity);
        return ResultVOUtil.success(areaService.findCityLevelByParentId(parentId));
    }

    @ResponseBody
    @RequestMapping("/getCountries")
    public ResultVO getCountries(@RequestParam("parentCity") String parentCity){
        Integer parentId = areaService.findAreaIdByName(parentCity);
        return ResultVOUtil.success(areaService.findCountryLevelByParentId(parentId));
    }
}
